<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#	
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
#	add_url.php : 
#
#		Renders form for compressing new urls. Sends ajax requests to check_availability.php . 
#		Included by ../compress_url.php if the user enters /add
##############################################################################################

ob_start();
?>
<html>
<head>
	<title>
		Compress URL
	</title>
	<!-- Favicon -->
	<link href="compress_url_libs/compress.gif" rel="icon" >
	<script language="javascript" type="text/javascript">

		// Sends POST var queryString through an AJAX request
		function ajax(queryString) {
			var xmlHttpReq = false;
			var self = this;
			
			// The URL where to POST
			var strURL = "compress_url_libs/check_availability.php";
			
			if (window.XMLHttpRequest) {
				// Mozilla/Safari
				self.xmlHttpReq = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				// IE
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			// Set up POST request
			self.xmlHttpReq.open('POST', strURL, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			// If got reply
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {
					// Refresh div check on getting reply
					document.getElementById("check").innerHTML = self.xmlHttpReq.responseText;
					// Hide progress img
					document.getElementById("progress").style.display = "none";
				}
			}
			
			// Send the POST request
			self.xmlHttpReq.send(queryString);
		}
	
		// Wrapper for ajax function
		function check(dontCheck) {
			// dontCheck is 0 for calls while pressing keys (onKeyUp) and 
			// 		1 for Check Availability button
			// dontCheck is the querystring for Compress button
			if ( document.getElementById("as_i_type").checked || dontCheck != 0 ) {
				toUrl = document.getElementById('to').value;
				from = document.getElementById('from').value;
				document.getElementById("progress").style.display = "inline";
//				document.getElementById("check").innerHTML = "";
				if(dontCheck == 0)
					dontCheck="";
				if(toUrl != "" || from!="") 
					ajax('to='+toUrl+'&from='+from+dontCheck);		
			}
		}
	</script>
	<link type="text/css" rel="stylesheet" href="compress_url_libs/style.css" />
</head>

<body onLoad="if(document.getElementById('<?php 
	// Focus on "to" input textfield of from is already filled by $get
	$from_or_to = isset($get) ? 'to' : 'from';
	echo $from_or_to;
	?>')) 
			document.getElementById('<?php echo $from_or_to;?>').focus();">
		
	<center>
		<div id="content">
			<?php 
				// Include authentication if required
				if ( $authmethod != "none")
					require_once("compress_url_auth.php"); 
				ob_flush();
				// Else go on
			?>		
			<h1><a class="title" href="http://code.google.com/p/compressurl">Compress URL</a></h1>
	
			<div id="bookmark">
				<a href="javascript:document.location=('<?php 
	
				// 	Bookmark link : Set the documents location to 
				//	<http://yourdomain.com/t/add?from=> + <the documents current location>
					if($MOD_REWRITE)
					
						// Send it as http://yourdomain.com/t/add? + <current location>
						echo get_path().$ADD_PATH."?"; 
						
					else
						// Send it as http://yourdomain.com/t/?q=add& + <current location>
						echo get_path().$ADD_PATH."&from="; 
						
				?>'+document.location);">
					Compress URL
				</a> from anywhere by bookmarking this link.
			</div>
			<form method="post" onSubmit="check('&compress=com'); return false;">
				<table border="0" cellpadding="5" cellspacing="5">
					<tr>
						<td class="label">
							<span>From: </span>
						</td>
						<td>
							<input onKeyUp="check(0)" type="text" id="from" size="50" value="<?= $get; ?>"/>
						</td>
					</tr>
					<tr>
						<td class="label">
							<span>To: </span>
						</td>
						<td>
							<?php echo get_path(); ?>
							<input type="text" id="to" size="33" onKeyUp="check(0)">
						</td>
					</tr>
					</table>
					
					<div id="check">
						
					</div>
						
					<table border="0" width="450">
						<tr>
							<td style="width:280px">
								<input type="button" value="Check Availability" onClick="check(1)" />
								<input type="checkbox" id="as_i_type" checked="checked" /> as I type
								<div id="progress" style="display:none"><img src="compress_url_libs/ajax.gif" /></div>
							</td>
							<td align="right">
								<input type="submit" name="compress" id="compress" value="Compress" />
								<input type="hidden" name="add" />
							</td>
						</tr>
				</table>
			</form>		
			<div id="footer">
				<table style="width:100%">
					<tr>
						<td align="left">
							<a href="http://www.gnu.org/licenses/lgpl.html" style="border:none; text-decoration:none;">
								<img src="compress_url_libs/gplv3.png" height="31" style="border:none;">
							</a>
						</td>
						<td align="right">
							<i>Developed by <a href="mailto:vermaabhishekp@gmail.com">Abhishek Verma</a></i>
						</td>
					</tr>
				</table>
			</div>
		</div>
	</center>
</body>
</html>